package com.vf.cloud.paas.common.log.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.app.master.common.domain.UserLog;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 */
@UtilityClass
public class SysLogUtils {

	public UserLog getSysLog() {
		HttpServletRequest request = ((ServletRequestAttributes) Objects
				.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
		
		UserLog log = new UserLog();
		log.setId(UuidUtil.getUUID());
		
		User user = WebUtils.getCurUser(request);
		if(!ObjectUtils.isEmpty(user)) {
			log.setCreateUserId(user.getCreateUserId());
		}
		log.setIp(ServletUtil.getClientIP(request));
		log.setUrl(URLUtil.getPath(request.getRequestURI()));
		log.setUseragent(request.getHeader(HttpHeaders.USER_AGENT));
		return log;
	}


}
